﻿using System;
using System.Collections.Generic;

namespace HgReleaseNotesGen
{
    using System.Globalization;
    using System.IO;

    internal class Changeset
    {
        public Changeset()
        {
            this.Parents = new List<ChangesetId>();
        }

        public ChangesetId Id { get; private set; }

        public List<ChangesetId> Parents { get; private set; }

        public string User { get; private set; }

        public DateTime Date { get; private set; }

        public string Summary { get; private set; }

        public string Branch { get; private set; }

        public static List<Changeset> LoadChangesetsFromStream(StreamReader stream)
        {
            var changesets = new List<Changeset>();

            while (!stream.EndOfStream)
            {
                var changeset = Changeset.LoadChangesetFromStream(stream);
                if (changeset != null)
                {
                    changesets.Add(changeset);
                }
            }

            return changesets;
        }

        public static Changeset LoadChangesetFromStream(StreamReader stream)
        {
            var done = false;
            var changeset = new Changeset();

            while (!stream.EndOfStream && !done)
            {
                var line = stream.ReadLine();
                
                if (line == null)
                {
                    break;
                }

                line = line.Trim();
                var lineStart = line.SubstringBefore(':').Trim();
                var lineEnd = line.SubstringAfter(':').Trim();

                switch (lineStart)
                {
                    case "changeset":
                        {
                            changeset.Id = new ChangesetId(lineEnd);
                        }
                        break;

                    case "parent":
                        {
                            changeset.Parents.Add(new ChangesetId(lineEnd));
                        }
                        break;

                    case "user":
                        {
                            changeset.User = lineEnd;
                        }
                        break;

                    case "summary":
                        {
                            changeset.Summary = lineEnd;
                            done = true;
                        }
                        break;

                    case "branch":
                        {
                            changeset.Branch = lineEnd;
                        }
                        break;

                    case "date":
                        {
                            DateTime result;

                            //TODO check datetime is getting the right value
                            if (DateTime.TryParseExact(lineEnd, "ddd MMM dd HH:mm:ss yyyy zzz", CultureInfo.InvariantCulture, DateTimeStyles.None,  out result))
                            {
                                changeset.Date = result;

                                var e = result.ToString("ddd MMM dd HH:mm:ss yyyy zzz");
                            }
                        }
                        break;
                }
            }

            return stream.EndOfStream ? null : changeset;
        }
    }
}
